var projectName = "/graduation_project";
var adminLimit = 1;
var operatorLimit = 2;
var normalLimit = 3;

$("table tr:nth-child(even)").addClass("trevencolor");

/**
 * 打开遮盖top和left
 */
function choose_open()
{
	window.top.document.getElementById("topFrame").contentWindow.document.getElementById("mid").style.display="block";
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("mid").style.display="block";
	
}
/**
 * 取消遮盖top和left
 */
function choose_close()
{
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("mid").style.display="none";
	window.top.document.getElementById("topFrame").contentWindow.document.getElementById("mid").style.display="none";
}

function left_choose_open()
{
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("mid_left").style.display="block";
}
function left_choose_close()
{
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("mid_left").style.display="none";
}

function log_left_show()
{
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("left_orgTree").style.display="none";
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("mid_left").style.display="none";
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("log_left").style.display="block";
}
function log_left_hide()
{
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("left_orgTree").style.display="block";
	window.top.document.getElementById("leftFrame").contentWindow.document.getElementById("log_left").style.display="none";
}

function getBodyWidth(){
	return document.body.clientWidth();
}
function getBodyHeight(){
	return document.body.clientHeight();
}

/**
 * 控制左侧导航菜单
 * @param name
 */
function toLeft(name)
{
	// 根据index取得左侧的二级菜单并展开二级菜单中的第一个菜单项
	var element = $(window.top.document.getElementById("leftFrame").contentWindow.document.getElementById(name + "Left"));
	element.parent().parent().find('ul').slideUp();
	element.next('ul').slideDown();
}
/**
 * 弹框提示 
 * 
 * @param type 0 :提示 1:成功 2:警告 3:错误 4:确认框
 * @param info 弹框信息
 * @param functionOk 确定回调函数
 * @param functionNo 回调函数(标志是否为确认框，没有实际的回调作用)
 * 
 */
function messageAlert(type,info,functionOk,functionNo)
{
	choose_open();
	var iconInfo = "";
	switch(type)
	{
	     case 0: iconInfo='info';break;
	     case 1: iconInfo='success';break;
		 case 2: iconInfo='warning';break;
		 case 3: iconInfo='error';break;
		 case 4: iconInfo='question';break;
		 default: return;
	}
	if(undefined == functionNo)
	{
		 // 自定义提示弹框
		 BUI.Message.Show({
		      msg : info,
		      icon : iconInfo,
		      cancel:function(){
		 		  choose_close();
		 	  },
			  buttons : [
		        {
			        text:'确定',
			        elCls : 'button button-primary',
			        handler : functionOk
	            }
		       ]
		  });
	}
	else
	{
		// 自定义提示弹框
		 BUI.Message.Show({
		      msg : info,
		      icon : iconInfo,
		      cancel:function(){
		 		  choose_close();
		 	  },
		 	  success:function(){
		 		 functionOk
		 	  },
			  buttons : [
		        {
			        text:'确定',
			        elCls : 'button button-primary',
			        handler : functionOk
			        	
	            },
		        {
		        	text:'取消',
		            elCls : 'button',
		            handler : function()
		            {
		            	choose_close();
		                this.close();
		            }
	            }
		      ]
		  });
	}
}
/******************************************************
						复选框								
******************************************************/
/**
 *	复选框全选
 */
function selectAllCheck(selectAll,selectCbx){
	var obj=document.getElementsByName(selectCbx);
	var selectAll=document.getElementById(selectAll);
	if(selectAll.checked==true)
	{
		for(var i=0;i<obj.length;i++)
		{
			obj[i].checked=true;
		}
	}
	else{
		for(var i=0;i<obj.length;i++)
		{
			obj[i].checked=false;
		}
	}
}
/**
 *	复选框选中事件
 */
function cbxCheck(selectAll,selectCbx){
	var obj=document.getElementsByName(selectCbx);
	var selectAll=document.getElementById(selectAll);
	var count=0;var i=0;
	for(i=0;i<obj.length;i++)
	{
		if(obj[i].checked==true)
		{
			count++;
		}
	}
	if(count==i)
	{
		selectAll.checked=true;
	}
	else
	{
		selectAll.checked=false;
	}
}
/**
 *	获取选中ID
 */
function getSelectCbx(selectCbx){
	var obj=document.getElementsByName(selectCbx);
	var selectIds="";
	for(i=0;i<obj.length;i++)
	{
		if(obj[i].checked==true)
		{
			var o=$(obj[i]).attr("id");
			var objId=$(obj[i]).attr("id").indexOf("_");
			var id=($(obj[i]).attr("id")).substring(objId+1);
			selectIds=selectIds+","+id;
		}
	}
	selectIds=selectIds.substring(1);
	return selectIds;
}

//获取全选且过滤 全选节点下的子节点
function getcommonTreeNodes(nodes)
{ 
	var pId="";
	var k=0;
	for(var i=0;i<nodes.length;i++)
	{
		if(k==1){
			i--;
			k=0;
		}
		if(""!=pId){
		if(nodes[i].pId.indexOf(pId)==0){
			nodes.splice(i,1);
			i--;
		}}
		if(nodes[i].check_Child_State==1){
			nodes.splice(i,1);
			if(i==0){
				k=1;
			}else{
				i--;
			}
		}
		if(nodes[i].isParent==true){
			if(nodes[i].check_Child_State==2){
				pId=nodes[i].id;
			}
		}
	}
	 return nodes;
}


/**
 *	字符串截取，用于table显示
 */
function getSubstr(str, len)
{
    var strlen = 0;
    var s = "";
    for (var i = 0; i < str.length; i++)
    {
        if (str.charCodeAt(i) > 128)
        {
            strlen += 2;
        }
        else
        {
            strlen++; 
        }
        s += str.charAt(i); 
        if (strlen >= len)
        {            
        	return s+"...";
        }
    }
    return s;
}

/***************************************************************
							左侧组织树
***************************************************************/
var left_org_orgname;
var left_zTreeObject;
var left_organ_setting = {
		view: {
			selectedMulti: false
		},
		data: {
			key: {
				title:"t"
			},
			simpleData: {
				enable: true
			}
		}
	};

var left_organ_zNodes =[];
/*
 * 当点击部门输入框时实时刷新组织列表，也可以只获取一次
 */
function getLeftOrganizationTreeNode()
{
	var url = cserverProjectName+"/organization/getAllOrganExcludeCascade.do?date="+new Date().getTime();
	$.ajax({
		url:url,
		async:false,
		success:function(data,status){
			left_organ_zNodes = [];
			for (var i = 0; i < data.length; i++) {
				var orgData = data[i];
				if(null == orgData.parentName)
				{
					left_organ_zNodes.push({ id:orgData.orgCode, pId:0, name:orgData.orgName, t:orgData.orgName, open:true ,url:cclientProjectName+"/PhoneUser/getPhoneUserList.do?orgCode="+orgData.orgCode ,target:"rightFrame",icon:"../css/img/org.png"});
				}
				else
				{
					//默认展开当前选中的节点的下级组织
					if(left_org_orgname == orgData.orgName)
					{
						left_organ_zNodes.push({ id:orgData.orgCode, pId:orgData.parentCode, name:orgData.orgName, t:orgData.orgName, open:true ,url:cclientProjectName+"/PhoneUser/getPhoneUserList.do?orgCode="+orgData.orgCode ,target:"rightFrame",icon:"../css/img/org.png"});
					}
					else
					{
						left_organ_zNodes.push({ id:orgData.orgCode, pId:orgData.parentCode, name:orgData.orgName, t:orgData.orgName, open:false ,url:cclientProjectName+"/PhoneUser/getPhoneUserList.do?orgCode="+orgData.orgCode ,target:"rightFrame",icon:"../css/img/org.png"});
					}
				}
			}
			$.fn.zTree.init($("#left_org_tree"), left_organ_setting, left_organ_zNodes);
		}
	});
}
/*
 * 显示部门树
 */
function showLeftOrgan_tree() 
{
	getLeftOrganizationTreeNode();
	$("#left_org_tree").show();
}

/**
 * Map
 * */
function Map() {  
    /** 存放键的数组(遍历用到) */  
    this.keys = new Array();  
    /** 存放数据 */  
    this.data = new Object();  
      
    /** 
     * 放入一个键值对 
     * @param {String} key 
     * @param {Object} value 
     */  
    this.put = function(key, value) {  
        if(this.data[key] == null){  
            this.keys.push(key);  
        }  
        this.data[key] = value;  
    };  
      
    /** 
     * 获取某键对应的值 
     * @param {String} key 
     * @return {Object} value 
     */  
    this.get = function(key) {  
        return this.data[key];  
    };  
      
    /** 
     * 删除一个键值对 
     * @param {String} key 
     */  
    this.remove = function(key) {  
        this.keys.remove(key);  
        this.data[key] = null;  
    };  
      
    /** 
     * 遍历Map,执行处理函数 
     *  
     * @param {Function} 回调函数 function(key,value,index){..} 
     */  
    this.each = function(fn){  
        if(typeof fn != 'function'){  
            return;  
        }  
        var len = this.keys.length;  
        for(var i=0;i<len;i++){  
            var k = this.keys[i];  
            fn(k,this.data[k],i);  
        }  
    }; 
    
    /** 
     * 遍历Map,执行处理函数,可添加参数 
     *  
     * @param {Function} 回调函数 function(key,value,index){..} 
     */  
    this.eachByParam = function(fn,param){  
        if(typeof fn != 'function'){  
            return;  
        }  
        var len = this.keys.length;  
        for(var i=0;i<len;i++){  
            var k = this.keys[i];  
            fn(k,this.data[k],i,param);  
        }  
    }; 
      
    /** 
     * 获取键值数组(类似Java的entrySet()) 
     * @return 键值对象{key,value}的数组 
     */  
    this.entrys = function() {  
        var len = this.keys.length;  
        var entrys = new Array(len);  
        for (var i = 0; i < len; i++) {  
            entrys[i] = {  
                key : this.keys[i],  
                value : this.data[i]  
            };  
        }  
        return entrys;  
    };  
      
    /** 
     * 判断Map是否为空 
     */  
    this.isEmpty = function() {  
        return this.keys.length == 0;  
    };  
      
    /** 
     * 获取键值对数量 
     */  
    this.size = function(){  
        return this.keys.length;  
    }; 
      
    /** 
     * 重写toString  
     */  
    this.toString = function(){  
        var s = "{";  
        for(var i=0;i<this.keys.length;i++,s+=','){  
            var k = this.keys[i];  
            s += k+"="+this.data[k];  
        }  
        s+="}";  
        return s;  
    };  
}

/**
 * 数组包含某个元素
 */
Array.prototype.containsArr = function(e){
	for(var i=0; i<this.length;i++){
		if(this[i]==e){
			return true;
		}
	}
	return false;
}


/**
*非输入框禁用退格键
*/
function banBackspace(e) {
    var ev = e || window.event;
    var field = ev.target || ev.srcElement;
    var fieldType = field.type || field.getAttribute('type');
    var isReadOnly = field.readOnly;
    var isDisabled = field.disabled;
    isReadOnly = (isReadOnly == undefined) ? false : isReadOnly;
    isDisabled = (isDisabled == undefined) ? true : isDisabled;
   
    var KEY_BACKSPACE = 8;
    var inputFieldTypes = [ 'password', 'text' , 'textarea'];
   
    var isOnReadonlyOrDisabledField = inputFieldTypes.containsArr(fieldType) && (isReadOnly || isDisabled);
    var isNotOnAnyField = !inputFieldTypes.containsArr(fieldType);
   
    if (ev.keyCode == KEY_BACKSPACE && (isOnReadonlyOrDisabledField || isNotOnAnyField)) {
         return false ;
    }
}
document.onkeydown = banBackspace;


function getSearch_mrn(type)
{
	var id = "#" + type + "_search_mrn";
	var search_mrn =  $(id).val();
	if(undefined == search_mrn||null == search_mrn)
	{
		search_mrn = "";
	}
	return search_mrn;
}

/**
*权限控制
*/
function pmsnControl()
{
	var pmsncontrol = $(".pmsncontrol");
	pmsncontrol.unbind("click");
	pmsncontrol.attr("onclick","");
	pmsncontrol.bind("click",function(){
		messageAlert(1,"您是普通用户，只有查询权限，请与管理员联系~",function()
				{
			this.close();
			choose_close();
		});
	});
}

/**
*当前日期格式化 YYYY-MM-DD HH:mm:ss
*/
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}

/**
 *	验证eno
 */
function findWorkByEno(eno){
	var flag = false;
	$.ajax({
		url: projectName + "/doc/findWorkByEno.do?date=" + new Date().getTime(),
		async:false,
		data:{"eno":eno},
		type:"post",
		success:function(result){
			if("SUCCESS" == result)
			{
		        flag=  true;
			}
			else if("ERROR" == result)
			{
				flag=  false; 
			}
			else
			{
				flag=  false; 
			}
			
		},
	});
	return flag ;
}

/**
 *	验证mrn
 */
function findPatByMrn(mrn)
{
	var flag = false;
	$.ajax({
		url: projectName + "/pat/getPatByMrn.do?date=" + new Date().getTime(),
		async:false,
		data:{"mrn":mrn},
		type:"post",
		success:function(result){
			if("null" != result && null !=result && result != "")
			{
		        flag = true;
			}
			else
			{
				flag = false; 
			}
			
		},
	});
	return flag ;
}
(function(){
	jQuery.extend(jQuery.validator.messages, { 
		required: "不能为空", 
		remote: "请修正该字段", 
		email: "请输入正确格式的电子邮件", 
		url: "请输入合法的网址", 
		date: "请输入合法的日期", 
		dateISO: "请输入合法的日期 (ISO).", 
		number: "数字不合法", 
		digits: "只能输入整数", 
		creditcard: "请输入合法的信用卡号", 
		equalTo: "请再次输入相同的值", 
		accept: "请输入拥有合法后缀名的字符串", 
		maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"), 
		minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"), 
		rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"), 
		range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"), 
		max: jQuery.validator.format("请输入一个最大为 {0} 的值"), 
		min: jQuery.validator.format("请输入一个最小为 {0} 的值") 
		}); 
})();

// 字符验证 
jQuery.validator.addMethod("stringCheck", function(value, element) { 
     return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value); 
  }, "只能包括中文字、英文字母、数字和下划线"); 

jQuery.validator.addMethod("isIdCardNo", function(value, element) { 
	  return this.optional(element) || idCardNoUtil.checkIdCardNo(value);     
	}, "请输入正确的身份证"); 
jQuery.validator.addMethod("theTrueMrn", function(value, element) {
	var flag = findPatByMrn(value);
	return this.optional(element) || (flag); 
	}, "请输入正确的病历号"); 

jQuery.validator.addMethod("theSameName", function(value, element) {
	return this.optional(element) || (findDocByName(value)); 
	}, "用户名已存在"); 

jQuery.validator.addMethod("theTrueEno", function(value, element) {
	return this.optional(element) || (findWorkByEno(value)); 
	}, "请输入正确的职工号"); 


